﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.IO;
using System.Text;
using System.Xml.Linq;
using AttributeRouting;
using AttributeRouting.Web.Mvc;

using EDTS.Logic.Weixin;
using EDTS.Weixin;
using EDTS.Model.Weixin;
namespace EDTS.WebSites.Services.Areas.Weixin.Controllers
{
    [RouteArea("Weixin")]
    public class ReceiverController : Controller
    {
        /// <summary>
        /// 与微信中心建立建立连接
        /// </summary>
        /// <param name="msg_signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        /// <returns></returns>
        [Route("Receiver/{Guid:guid}")]
        [HttpGet]
        public string Receiver(Guid guid, string msg_signature, string timestamp, string nonce, string echostr)
        {
            try
            {
                var app = WxAppLogic.GetModel(guid);
                if (app != null)
                {
                    var crypt = new WXBizMsgCrypt(app.Token, app.EncodingAESKey, app.CorpID);
                    string ret = null;
                    var code = crypt.VerifyURL(msg_signature, timestamp, nonce, echostr, ref ret);
                    if (code == 0)
                        return ret;
                }
            }
            catch { }
            return null;
        }
        /// <summary>
        /// 接收微信中心的消息
        /// </summary>
        /// <param name="msg_signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <returns></returns>
        [Route("Receiver/{Guid:guid}")]
        [HttpPost]
        public string Receiver(Guid guid, string msg_signature, string timestamp, string nonce)
        {
            try
            {
                var app = WxAppLogic.GetModel(guid);
                if (app != null)
                {
                    var crypt = new WXBizMsgCrypt(app.Token, app.EncodingAESKey, app.CorpID);
                    var postData = new StreamReader(Request.InputStream).ReadToEnd();
                    string msg = null;
                    var code = crypt.DecryptMsg(msg_signature, timestamp, nonce, postData, ref msg);
                    if (code == 0)
                    {
                        var request = MessageFactory.GetRequest(XDocument.Parse(msg));
                        var req = new WxRequestModel()
                        {
                            MsgID = request.MsgId,
                            ToUserName = request.ToUserName,
                            FromUserName = request.FromUserName,
                            MsgType = request.MsgType.ToString(),
                            Description = msg
                        };
                        WxRequestLogic.CreateNew(req);
                    }
                }
            }
            catch (Exception ex) {
                Common.Logger.Error("", ex);
            }
            return null;
        }
    }
}
